/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Spacer;
import util.J2meAppHelper;
import map.GoogleMaps;

/**
 *
 * @author victorinox
 */
public class MapForm extends Form implements AppForm, CommandListener, ItemCommandListener {

    private RegDetailVO reg = null;
    private Command backCmd = new Command("Voltar", Command.BACK, 0);
    private Command aproximarCmdItem = new Command("Aproximar", Command.ITEM, 0);
    private Command afastarCmd = new Command("Afastar", Command.SCREEN, 3);
    private int zoom = 15;
    private static MapForm instance = new MapForm();

    public static MapForm getInstance() {
        return instance;
    }

    protected MapForm() {
        super("Aguardando mapa...");
        addCommand(afastarCmd);
        addCommand(backCmd);
        setCommandListener(this);
    }

    public Displayable getDisplyable() {
        return this;
    }

    public void initialize(Object parameter) {

        if (parameter instanceof RegDetailVO) {
            reg = (RegDetailVO) parameter;
        }
        generateMap();
    }

    private void generateMap() {

        for (int i = 0; i < size(); i++) {
            delete(i);
        }

        new Thread(new Runnable() {

            public void run() {
                try {
                    GoogleMaps m = new GoogleMaps();

                    String endereco = "";

                    endereco = reg.getRegistro().getEndereco() + ", " + reg.getRegistro().getCidade() + ", " + reg.getRegistro().getUF() + ", brasil";

                    String[] latLong = m.geocodeAddress(endereco);
                    int height = J2meAppHelper.getInstance().getMidlet().getDisplay().getCurrent().getHeight();
                    int width = J2meAppHelper.getInstance().getMidlet().getDisplay().getCurrent().getWidth();
                    height += (height * 10) / 100;
                    Image img = m.retrieveStaticImage(width, height, latLong[0], latLong[1], zoom, "PNG32");
                    ImageItem imgItem = new ImageItem("", img, Item.LAYOUT_CENTER, "", Item.PLAIN);

                    imgItem.setDefaultCommand(aproximarCmdItem);

                    imgItem.setItemCommandListener(getInstance());

                    setTitle(reg.getRegistro().getNome());

                    append(imgItem);

                } catch (Exception ex) {
                    if (ex instanceof IllegalArgumentException) {
                        append("Erro ao formatar a imagem.");
                    } else {
                        append(ex.getMessage());
                    }
                    ex.printStackTrace();
                }
            }
        }).start();

    }

    public void commandAction(Command arg0, Displayable arg1) {

        if (arg0.equals(afastarCmd)) {
            if (zoom >= 1) {
                zoom -= 1;
                generateMap();
            }
        } else {
            for (int i = 0; i < size(); i++) {
                delete(i);
            }
            J2meAppHelper.getInstance().changeForm(new DetalheRegistroForm(), reg);
        }

    }

    public void commandAction(Command arg0, Item arg1) {

        if (arg0.equals(aproximarCmdItem)) {
            if (zoom <= 16) {
                zoom += 1;
                generateMap();
            }
        }
    }
}
